package com.yami.shop.webservice.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yami.shop.bean.enums.PaySeviceTypeCode;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.DataWebService;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.service.UserService;
import com.yami.shop.webservice.common.model.*;
import com.yami.shop.webservice.common.service.CopyrightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


@Service
public class CopyrightServiceImpl implements CopyrightService {
    @Autowired
    private ShopConfig shopConfig;
    @Autowired
    private DataWebService dataWebService;
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private Map<String, String> beanToMap(Object bean){
        BeanMap beanMap = BeanMap.create(bean);
        Map<String, String> map = new HashMap<>();
        beanMap.forEach((key, value) -> {
            if(!ObjectUtil.isEmpty(value)){
                map.put(String.valueOf(key), String.valueOf(value));
            }
        });
        return map;
    }

    @Override
    public JSONObject foreign(RegisterForeignDto foreignDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/foreign?";
        log.info("foreign参数"+ JSON.toJSONString(foreignDto));
        Map<String, String> stringObjectMap = beanToMap(foreignDto);
        String result = dataWebService.hutoolPostWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject fileUpload(AttachmentUploadDto attachmentUploadDto,File file) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/fileUpload?";
//        String apiGatewayUrl = "http://192.168.1.239:9208/outapi/copyright/fileUpload";
//        log.info("fileUpload参数"+ JSON.toJSONString(attachmentUploadDto));
        attachmentUploadDto.setFile(null);
        Map<String, String> stringObjectMap = beanToMap(attachmentUploadDto);
        String result = dataWebService.hutoolPostFileWebService(apiGatewayUrl,stringObjectMap,file);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject addOwner(OwnerDto ownerDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/addOwner?";
        log.info("addOwner参数"+ JSON.toJSONString(ownerDto));
        Map<String, String> stringObjectMap = beanToMap(ownerDto);
        String result = dataWebService.hutoolPostWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject getOwnersList(OwnersListDto ownersListDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/getOwnersList?";
        log.info("getOwnersList参数"+ JSON.toJSONString(ownersListDto));
        Map<String, String> stringObjectMap = beanToMap(ownersListDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject getOwnersById(OwnersByIdDto ownersByIdDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/getOwnersById?";
        log.info("getOwnersById参数"+ JSON.toJSONString(ownersByIdDto));
        Map<String, String> stringObjectMap = beanToMap(ownersByIdDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONArray getDictByType(DictItemDto dictItemDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/dict?";
//        String apiGatewayUrl = "http://192.168.1.239:9208/outapi/copyright/dict?";
        log.info("getDictByType参数"+ JSON.toJSONString(dictItemDto));
        Map<String, String> stringObjectMap = beanToMap(dictItemDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                return jsonObject.getJSONArray("data");
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject workSave(WorksInstDto worksInstDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/workSave?";
        log.info("workSave参数"+ JSON.toJSONString(worksInstDto));
        Map<String, String> stringObjectMap = beanToMap(worksInstDto);
        String result = dataWebService.hutoolPostWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject workUpdate(WorksInstDto worksInstDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/workUpdate?";
        log.info("workUpdate参数"+ JSON.toJSONString(worksInstDto));
        Map<String, String> stringObjectMap = beanToMap(worksInstDto);
        String result = dataWebService.hutoolPostWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject getWorksInstPage(WorkInstPageQueryDto workInstPageQueryDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/workPage?";
        log.info("getWorksInstPage参数"+ JSON.toJSONString(workInstPageQueryDto));
        Map<String, String> stringObjectMap = beanToMap(workInstPageQueryDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject getWorkByAcceptNo(WorkByAcceptNoDto workByAcceptNoDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/getWorkByAcceptNo?";
        log.info("getWorkByAcceptNo参数"+ JSON.toJSONString(workByAcceptNoDto));
        Map<String, String> stringObjectMap = beanToMap(workByAcceptNoDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject getWorkProgressByAcceptNo(WorkByAcceptNoDto workByAcceptNoDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/getWorkProgressByAcceptNo?";
        log.info("getWorkProgressByAcceptNo参数"+ JSON.toJSONString(workByAcceptNoDto));
        Map<String, String> stringObjectMap = beanToMap(workByAcceptNoDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject depositWorksCertificate(WorkByAcceptNoDto workByAcceptNoDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/worksEvidence?";
        log.info("depositWorksCertificate参数"+ JSON.toJSONString(workByAcceptNoDto));
        Map<String, String> stringObjectMap = beanToMap(workByAcceptNoDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject registrationWorksCertificate(WorkByAcceptNoDto workByAcceptNoDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/worksRegistration?";
        log.info("registrationWorksCertificate参数"+ JSON.toJSONString(workByAcceptNoDto));
        Map<String, String> stringObjectMap = beanToMap(workByAcceptNoDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }

    @Override
    public JSONObject worksRegistrationData(WorkByAcceptNoDto workByAcceptNoDto) {
        String apiGatewayUrl = shopConfig.getApiGatewayUrl()+"/webservice/outapi/copyright/worksRegistrationData?";
        log.info("workByAcceptNoDto参数"+ JSON.toJSONString(workByAcceptNoDto));
        Map<String, String> stringObjectMap = beanToMap(workByAcceptNoDto);
        String result = dataWebService.hutoolGetWebService(apiGatewayUrl,stringObjectMap);
        JSONObject jsonObject =  JSON.parseObject(result);
        if (null != jsonObject) {
            if (Constant.SUCCESS_CODE==jsonObject.getInteger("code")) {
                JSONObject ress = jsonObject.getJSONObject("data");
                return ress;
            }else {
                log.error(jsonObject.getString("msg"));
                throw new MailException(jsonObject.getString("msg"));
            }
        }else {
            log.error("第三方发起接口返回空");
            throw new MailException("第三方发起接口返回空");
        }
    }
}
